package org.itboys.baseobject.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.itboys.baseobject.dto.KeyValuesTreeConfigDto;
import org.itboys.baseobject.entity.KeyValuesTreeConfig;
import org.itboys.baseobject.service.KeyValuesTreeConfigService;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mongodb.utils.query.QueryParamUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 树形的 key values config
 * @author weisky
 *
 */
@RestController 
@RequestMapping("/kvConfigTree") 
public class KeyValuesTreeConfigController extends BaseController{

	@Autowired
	private KeyValuesTreeConfigService keyValuesTreeConfigService;
	
	@RequestMapping("/list") 
	public void list(HttpServletRequest request, HttpServletResponse response){
		Map<String, Object> param = QueryParamUtils.builderQueryMap(request);
		List<KeyValuesTreeConfigDto> list = keyValuesTreeConfigService.listOvv(param);
		JsonPageUtils.renderJsonPage(Long.valueOf(list.size()), list, response);
	}
	
	@RequestMapping("/getById") 
	public void getById(@RequestParam("id") Long id,
			HttpServletRequest request, HttpServletResponse response){
		AjaxUtils.renderJson(response, keyValuesTreeConfigService.getById(id));
	}
	
	@RequestMapping("/save") 
	public void save(KeyValuesTreeConfig keyValuesTreeConfig,
			HttpServletRequest request, HttpServletResponse response){
		try{
			keyValuesTreeConfigService.save(keyValuesTreeConfig);
			AjaxUtils.renderJson(response, 1);
		}catch(Exception e){
			e.printStackTrace();
			logger.error("KeyValuesTreeConfigController.save error", e);
			AjaxUtils.renderJson(response, -1);
		}
	}
	
	@RequestMapping("/delete")
	public void delete(@RequestParam("id") Long id,
			HttpServletRequest request, HttpServletResponse response){
		try{
			keyValuesTreeConfigService.delete(id);
			AjaxUtils.renderJson(response, 1);
		}catch(Exception e){
			e.printStackTrace();
			logger.error("KeyValuesTreeConfigController.delete error", e);
			AjaxUtils.renderJson(response, -1);
		}
	}
}
